package com.mszlu.blog.config;

import com.mszlu.blog.handler.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.websocket.OnClose;

/**
 * Created with IntelliJ IDEA.
 * User: SJP
 * Date: 2022/2/25
 * Time: 21:20
 *
 * @author sjp
 * Description: 前端是8080端口，而服务器启动后端的是8888端口，  不同服务  要进行跨域访问，
 */
@Configuration
public class WebMVCConfig implements WebMvcConfigurer {
    @Autowired
    private LoginInterceptor loginInterceptor;
    @Override
    public void addCorsMappings(CorsRegistry registry){
        //跨域配置，不可设置为*，不安全, 前后端分离项目，可能域名不一致
        //本地测试 端口不一致 也算跨域
        registry.addMapping("/**").allowedOrigins("http://localhost:8080");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry){
        //假设拦截test接口后续实际遇到拦截的接口是时，再配置真正的拦截接口
        /*registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**").excludePathPatterns("login").excludePathPatterns("regist");*/
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/test")
                //.addPathPatterns("comments/create/change");少了/，则对/comments/create/change不会进行拦截
                //则不会把UserThreadLocal.put(sysUser);用户信息存入UserThreadLocal中，则SysUser就为null
                .addPathPatterns("/comments/create/change")
                .addPathPatterns("/articles/publish");

    }
}
